package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ass.beans.CinemaBean;
import ass.beans.MovieBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;

public class CinemaDAOImpl {
	
	/**
	 * The service locator to retrieve database connections from
	 */
	private DBConnectionFactory services;
	
	/** Creates a new instance of UserDAOImpl */
	public CinemaDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public CinemaDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}

	public void insert(CinemaBean bean) throws DataAccessException {
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO TBL_CINEMAS (CINEMA_LOCATION, CINEMA_SEATING_CAPACITY,HAS_ATM,HAS_WIDESCREEN,HAS_SNACK_BAR,HAS_RESTAURANT) values ( ?, ?, ?, ?, ?, ?)");
	     
	     stmt.setString(1, bean.getCinema_location());
	     stmt.setString(2, bean.getCinema_seating_capacity());
	     stmt.setString(3, bean.getHas_atm());
	     stmt.setString(4, bean.getHas_widescreen());
	     stmt.setString(5, bean.getHas_snack_bar());
	     stmt.setString(6, bean.getHas_restaurant());

	     
//CINEMA_ID, CINEMA_LOCATION, CINEMA_SEATING_CAPACITY,HAS_ATM,HAS_WIDESCREEN,HAS_SNACK_BAR,HAS_RESTAURANT

	     
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
	}

	public List<CinemaBean> getCinema() throws DataAccessException{
		
		
		List<CinemaBean> list = new ArrayList<CinemaBean>();
		Connection con = null;
		try {
			con = services.createConnection();

			PreparedStatement stmt = con
					.prepareStatement("select * from TBL_CINEMAS");


			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				CinemaBean cinemabean = createCinemaBean(rs);
				list.add(cinemabean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
		
	}

	private CinemaBean createCinemaBean(ResultSet rs) throws SQLException {
		CinemaBean cinemaBean = new CinemaBean();

		cinemaBean.setCinema_id(rs.getInt("cinema_id"));
		cinemaBean.setCinema_location(rs.getString("cinema_location"));


		return cinemaBean;
	}
	
	
}
